package com.fe.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.fe.model.Patriarch;
import com.fe.service.PatriarchService;
import com.fe.util.JacksonData;
import com.fe.util.UploadFile;

@Controller
@RequestMapping("/patriarch")
public class PatriarchController {
	
	@Autowired
	private PatriarchService patriarchService;
	
	/**
	 * 
	 * 方法描述：跳转到家长个人信息界面
	 * @return
	 * @author ssh
	 * @date 2018年8月21日 下午11:44:38
	 */
	@GetMapping("/getPatriarch")
	public String getPatriarch() {
		return "patriarch/getPatriarch";
	}
	
	/**
	 * 
	 * 方法描述：获取一个家长更新的数据，进行更新
	 * @param map
	 * @param teacher
	 * @return
	 * @author ssh
	 * @date 2018年8月21日 下午11:44:38
	 */
	@PostMapping("/getPatriarch")
	public String updateTeacher(HttpServletRequest request, Map<String, Object> map, Patriarch patriarch) {
		patriarchService.update(request, map, patriarch);
		return "patriarch/getPatriarch";
	}
	
	/**
	 * 
	 * 方法描述：家长修改账号时，判断账号是否已经被使用
	 * @param account
	 * @return
	 * @author ssh
	 * @date 2018年8月22日 下午8:43:58
	 */
	@PostMapping("/existAccount")
	@ResponseBody
	public JacksonData existAccount(String account) {
		JacksonData jd = new JacksonData();
		try {
			patriarchService.exictAccount(account);
			jd.success();
		} catch (Exception e) {
			jd.failure(e.getMessage());
		}
		return jd;
	}
	
	/**
	 * 
	 * 方法描述：跳到家长上传身份证界面
	 * @return
	 * @author ssh
	 * @date 2018年8月25日 下午9:47:08
	 */
	@GetMapping("/uploadPatriarch")
	public String uploadPatriarch() {
		return "patriarch/uploadPatriarch";
	}
	
	/**
	 * 
	 * 方法描述：家长上传身份证
	 * @param request
	 * @param map
	 * @param patriarch
	 * @param pat_identity_card_t
	 * @return
	 * @author ssh
	 * @date 2018年8月25日 下午4:58:31
	 */
	@PostMapping("/uploadPatriarch")
	public String uploadPatriarch(HttpServletRequest request, Map<String, Object> map, Patriarch patriarch, MultipartFile pat_identity_card_t) {
		try {
			String newName = UploadFile.upload(pat_identity_card_t);
			patriarch.setPat_identity_card(newName);
			patriarchService.update(request, map, patriarch);
			map.put("message", "上传成功!");
		} catch (IllegalStateException | IOException e) {
			map.put("message", "上传失败!");
		}
		return "patriarch/uploadPatriarch";
	}
	/**
	 * 
	 * 方法描述：改变路径跳转到个人信息
	 * @return
	 * @author ssh
	 * @date 2018年9月23日 上午11:23:01
	 */
	@GetMapping("/addMoney")
	public String addMoeny() {
		return "redirect:/patriarch/getPatriarch";
	}
	
	/**
	 * 
	 * 方法描述：充值
	 * @param request
	 * @param map
	 * @param patriarch
	 * @return
	 * @author ssh
	 * @date 2018年9月23日 上午11:23:22
	 */
	@PostMapping("/addMoney")
	public String addMoney(HttpServletRequest request, Map<String, Object> map, Patriarch patriarch) {
		try {
			HttpSession session = request.getSession();
			Patriarch patriarch2 = (Patriarch) session.getAttribute("patriarch");
			int balance = patriarch2.getPat_balance();
			patriarch.setPat_balance(patriarch.getPat_balance()+balance);
			patriarchService.update(request, map, patriarch);
			map.put("message", "充值成功!");
		} catch (Exception e) {
			map.put("message", "充值失败!");
		}
		return "patriarch/getPatriarch";
	}
	
}
